package com.tuanbiao.ui.setting;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.controllers.SettingController;
import com.tuanbiao.data.entity.ZBServiceItem;
import com.tuanbiao.databinding.ActivitySettingZbServerBinding;
import com.tuanbiao.dialog.InsertZBDialog;
import com.tuanbiao.dialog.SelectDialog;
import com.tuanbiao.models.SettingModel;
import com.tuanbiao.presenter.setting.ZbServerChangePresenter;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class SettingZbServerActivity extends BasePresenterActivity<ZbServerChangePresenter, ActivitySettingZbServerBinding> implements SettingController.ZbServerChangeView {
    private ZBServiceItem zbServiceItem;

    public static void start(Context context) {
        Intent starter = new Intent(context, SettingZbServerActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected ActivitySettingZbServerBinding getBindingView() {
        return ActivitySettingZbServerBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        binding.ivToolbarBack.setOnClickListener(getBackListener());
//        settingZBServiceAdapter = new SettingZBServiceAdapter();
//        settingZBServiceAdapter.setOnItemClickListener((adapter, view, position) -> {
//            ZbServiceEntity entity = (ZbServiceEntity) adapter.getItem(position);
//            mPresenter.selectService(entity);
//        });
//        binding.rvContent.setLayoutManager(new LinearLayoutManager(getActivity()));
//        binding.rvContent.setAdapter(settingZBServiceAdapter);
        binding.sbChoose.setOnClickListener(v -> {
            mPresenter.showServiceSelect();
        });
        binding.ivAdd.setOnClickListener(v -> {
            showAddZbDialog();
        });
        binding.btnUpdate.setOnClickListener(v -> {
            if (null != zbServiceItem) {
                if (TextUtils.isEmpty(getString(binding.cetName))) {
                    showMessage("请输入名称");
                    return;
                }
                zbServiceItem.setName(getString(binding.cetName));
                if (TextUtils.isEmpty(getString(binding.cetIp))) {
                    showMessage("请输入中爆网地址");
                    return;
                }
                zbServiceItem.setIp(getString(binding.cetIp));
                if (TextUtils.isEmpty(getString(binding.cetPort))) {
                    showMessage("请输入中爆网端口");
                    return;
                }
                zbServiceItem.setPort(Integer.parseInt(getString(binding.cetPort)));
                mPresenter.updateZbService(zbServiceItem);
            }
        });
        binding.btnUseState.setOnClickListener(v -> {
            mPresenter.removeZBIp();
        });
    }

    private void showAddZbDialog() {
        InsertZBDialog.newInstance()
                .setOnConfirmListener((name, ip, port) -> mPresenter.insertZbService(new ZBServiceItem(1,ip,name,Integer.parseInt(port))))
                .show(getSupportFragmentManager(), "InsertZBDialog");
    }

    @Override
    protected ZbServerChangePresenter createPresenter() {
        return new ZbServerChangePresenter(new SettingModel(), this);
    }

    @Override
    public void showServices(ZBServiceItem entity) {
        zbServiceItem = entity;
        if (null != entity) {
            binding.cetName.setText(entity.getName());
            binding.cetIp.setText(entity.getIp());
            binding.cetPort.setText(String.valueOf(entity.getPort()));
        }else {
            binding.cetName.setText("");
            binding.cetIp.setText("");
            binding.cetPort.setText("");
        }
    }

    @Override
    public void showSelectDialog(List<ZBServiceItem> items, int selectPosition) {
        // 单选对话框
        SelectDialog.Builder builder = new SelectDialog.Builder(getActivity())
                .setTitle("选择服务器")
                .setList(items)
                .setSelect(selectPosition)
                // 设置单选模式
                .setSingleSelect();
        // 设置默认选中
        builder.setListener((SelectDialog.OnListener<ZBServiceItem>) (dialog, data) -> {
            Set<Map.Entry<Integer, ZBServiceItem>> entries = data.entrySet();
            for (Map.Entry<Integer, ZBServiceItem> entry : entries) {
                zbServiceItem = entry.getValue();
            }
            if (null != zbServiceItem) {
                mPresenter.selectService(zbServiceItem);
                showServices(zbServiceItem);

            }
        }).show();
    }
}